/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import org.evswork.getwit.smackwrap.SmackWrapper;
import org.evswork.getwit.smackwrap.Talk;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class TitleBarWrapper {
	GttMainActivity act;
	ProgressBar pgBar;
	TextView TitleText;
	LinearLayout TitleLayout;
	
	public TitleBarWrapper(GttMainActivity act) {
		this.act = act;
		bindView();
	}

	private void bindView() {
		TitleLayout = (LinearLayout)act.findViewById(R.id.mytitlebar);
		pgBar = (ProgressBar)TitleLayout.findViewById(R.id.pg_bar);
		TitleText = (TextView)TitleLayout.findViewById(R.id.title_text);
		TitleText.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				new AlertDialog.Builder(act)
					.setTitle("消息/连接控制")
					.setItems(
						new String [] {"消息模式","接收间隔","断开重连"},
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(
									DialogInterface dialog,
									int which) {
								switch (which) {
								case 0: // mode
			{
				new AlertDialog.Builder(act)
				.setTitle("选择模式:")
				.setItems(
					new String [] {"-on 完全接收","-self 接收回复和DM","-off 不接收"},
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(
								DialogInterface dialog,
								int which) {
							Talk.send(
								0 == which ? "-on":
								1 == which ? "-self":
								2 == which ? "-off": "");
							dialog.dismiss();
						}
						
					})
				.show();
			}
									
									break;
								case 1: // interval
			{
				new AlertDialog.Builder(act)
				.setTitle("消息接收间隔:")
				.setItems(act.getResources().getStringArray(R.array.interval_time),
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Talk.send(String.format("-time %d", which +1));
							dialog.dismiss();
						}
					}
				)
				.show();
			}
									break;
								case 2: // break connection
									SmackWrapper.getInstance().disconnect();
									break;
								}
							}
						}
					)
					.show();
				return true;
			}
			
		});
	}

	public void showProgressBar() {
		act.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				pgBar.setVisibility(View.VISIBLE);
			}
			
		});
	}
	
	public void hideProgressBar() {
		act.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				pgBar.setVisibility(View.GONE);
			}
			
		});
	}
}
